package cn.stronglink.assetmanage.business.check.assetlist;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.request.RequestOptions;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import cn.stronglink.assetmanage.R;
import cn.stronglink.assetmanage.entity.Asset;
import cn.stronglink.assetmanage.listener.onRecyclerListener;
import cn.stronglink.assetmanage.utils.ImageUtils;

/**
 * Author by chencongcong,Date on 2018/9/20
 */
public class UnCheckAssetAdapter extends RecyclerView.Adapter<UnCheckAssetAdapter.UnCheckAssetHolder> {
    private List<Asset> mAssetList;
    private Context mContext;
    private onRecyclerListener mListener;
    private OnItemCheckedChangedListener onItemCheckedChangedListener;
    private OnItemScanRfidListener onItemScanRfidListener;
    private Map<Integer, UnCheckAssetHolder> map = new HashMap<>();

    public void setListener(onRecyclerListener listener) {
        mListener = listener;
    }

    public void setOnItemCheckedChangedListener(OnItemCheckedChangedListener onItemCheckedChangedListener) {
        this.onItemCheckedChangedListener = onItemCheckedChangedListener;
    }

    public void setOnItemScanRfidListener(OnItemScanRfidListener onItemScanRfidListener) {
        this.onItemScanRfidListener = onItemScanRfidListener;
    }

    public UnCheckAssetAdapter(Context context) {
        mContext = context;
    }

    @Override
    public UnCheckAssetAdapter.UnCheckAssetHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        LayoutInflater inflater = LayoutInflater.from(mContext);
        View itemView = inflater.inflate(R.layout.item_uncheck_asset_list, null, true);
        RecyclerView.LayoutParams lp = new RecyclerView.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
                60);
        itemView.setLayoutParams(lp);
        return new UnCheckAssetAdapter.UnCheckAssetHolder(itemView, mListener, onItemCheckedChangedListener,
                onItemScanRfidListener);
    }

    @Override
    public void onBindViewHolder(UnCheckAssetAdapter.UnCheckAssetHolder holder, int position) {
        map.put(position, holder);
        if (mAssetList != null) {
            if (position % 2 == 0) {
                //偶数
                holder.ll_uncheck_item.setBackgroundColor(mContext.getResources().getColor(R.color.colorWhite));
            } else {
                holder.ll_uncheck_item.setBackgroundColor(mContext.getResources().getColor(R.color.gray_check_item));
            }
            holder.tvUnCheckSerialNo.setText(String.valueOf(position + 1));
            holder.tvUnCheckAssetNo.setText(mAssetList.get(position).getAssetNo());
            holder.tvUnCheckAssetName.setText(mAssetList.get(position).getAssetName());
            String rfidNo = mAssetList.get(position).getRfidNo();
            if (rfidNo != null && !"".equals(rfidNo)) {
                holder.tvUnCheckAssetRfid.setText(rfidNo);
                holder.tvUnCheckAssetRfid.setBackground(null);
                holder.tvUnCheckAssetRfid.setTextColor(mContext.getResources().getColor(R.color.black_check_in));
            }else{
                holder.tvUnCheckAssetRfid.setText("标签绑定");
                holder.tvUnCheckAssetRfid.setBackground(
                        mContext.getResources().getDrawable(R.drawable.bg_button_detail_select));
                holder.tvUnCheckAssetRfid.setTextColor(mContext.getResources().getColor(R.color.white));

            }
            holder.tvUnCheckAssetLocation.setText(mAssetList.get(position).getRoomName());
            holder.tvUnCheckAssetSn.setText(mAssetList.get(position).getSerialNo());
            holder.ivUnCheckAssetPicture.setVisibility(View.INVISIBLE);
            String url = mAssetList.get(position).getPictureUri();
            if (url != null) {
                holder.ivUnCheckAssetPicture.setVisibility(View.VISIBLE);
                RequestOptions options = new RequestOptions()
                        .centerCrop()
                        .placeholder(R.color.color_f6)
                        .diskCacheStrategy(DiskCacheStrategy.ALL);
                Glide.with(mContext)
                        .load(url)
                        .apply(options)
                        .into(holder.ivUnCheckAssetPicture);
            }
        }
    }

    @Override
    public int getItemCount() {
        return mAssetList == null ? 0 : mAssetList.size();
    }

    public class UnCheckAssetHolder extends RecyclerView.ViewHolder implements
            View.OnClickListener, CompoundButton.OnCheckedChangeListener {
        private onRecyclerListener mListener;
        private OnItemCheckedChangedListener onItemCheckedChangedListener;
        private OnItemScanRfidListener onItemScanRfidListener;
        @BindView(R.id.cb_uncheck)
        CheckBox cbUncheck;
        @BindView(R.id.tv_uncheck_serial_no)
        TextView tvUnCheckSerialNo;
        @BindView(R.id.tv_uncheck_asset_no)
        TextView tvUnCheckAssetNo;
        @BindView(R.id.tv_uncheck_asset_name)
        TextView tvUnCheckAssetName;
        @BindView(R.id.tv_uncheck_asset_rfid)
        TextView tvUnCheckAssetRfid;
        @BindView(R.id.tv_uncheck_asset_location)
        TextView tvUnCheckAssetLocation;
        @BindView(R.id.tv_uncheck_asset_sn)
        TextView tvUnCheckAssetSn;
        @BindView(R.id.iv_uncheck_asset_picture)
        ImageView ivUnCheckAssetPicture;
        @BindView(R.id.ll_uncheck_item)
        LinearLayout ll_uncheck_item;

        public UnCheckAssetHolder(View itemView, onRecyclerListener listener,
                                  OnItemCheckedChangedListener onItemCheckedChangedListener,
                                  OnItemScanRfidListener onItemScanRfidListener) {
            super(itemView);
            this.mListener = listener;
            this.onItemCheckedChangedListener = onItemCheckedChangedListener;
            this.onItemScanRfidListener = onItemScanRfidListener;
            ButterKnife.bind(this, itemView);
            itemView.setOnClickListener(this);
            cbUncheck.setOnCheckedChangeListener(this);
            tvUnCheckAssetRfid.setOnClickListener(this);
        }

        @Override
        public void onClick(View v) {
            if (v instanceof TextView) {
                if (onItemScanRfidListener != null) {
                    onItemScanRfidListener.onItemScanRfid(v, getAdapterPosition());
                }
            } else {
                if (mListener != null) {
                    mListener.onItemClicked(v, getAdapterPosition());
                }
            }

        }

        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            if (onItemCheckedChangedListener != null) {
                onItemCheckedChangedListener.onItemCheckedChanged(buttonView, isChecked, getAdapterPosition());
            }
        }
    }


    public void notifyDataChanged(List<Asset> mAssetList) {
        this.mAssetList = mAssetList;
        this.notifyDataSetChanged();
    }

    public interface OnItemCheckedChangedListener {
        /**
         * 点击选择或者取消选择
         *
         * @param buttonView
         * @param isChecked
         * @param position
         */
        void onItemCheckedChanged(CompoundButton buttonView, boolean isChecked, int position);
    }

    public interface OnItemScanRfidListener {
        /**
         * 点击扫描RFID
         *
         * @param view
         * @param position
         */
        void onItemScanRfid(View view, int position);
    }

    public void setAllCheck(boolean isCheck) {
        if (map == null) {
            return;
        }
        for (int i = 0; i < map.size(); i++) {
            UnCheckAssetHolder viewholder = map.get(i);
            viewholder.cbUncheck.setChecked(isCheck);
        }
    }

}
